package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.platform.entity.BasicOpenapiEntity;
import com.iwomy.secureplat.platform.service.BasicOpenapiService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * OpenAPI对接表
 *
 * @author pig
 * @date 2023-11-06 15:41:41
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicOpenapi" )
@Tag(description = "basicOpenapi" , name = "OpenAPI对接表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicOpenapiController {

    private final  BasicOpenapiService basicOpenapiService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param basicOpenapi OpenAPI对接表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('platform_basicOpenapi_view')" )
    public R getBasicOpenapiPage(@ParameterObject Page page, @ParameterObject BasicOpenapiEntity basicOpenapi) {
        LambdaQueryWrapper<BasicOpenapiEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(basicOpenapiService.page(page, wrapper));
    }


    /**
     * 通过id查询OpenAPI对接表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('platform_basicOpenapi_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(basicOpenapiService.getById(id));
    }

    /**
     * 新增OpenAPI对接表
     * @param basicOpenapi OpenAPI对接表
     * @return R
     */
    @Operation(summary = "新增OpenAPI对接表" , description = "新增OpenAPI对接表" )
    @SysLog("新增OpenAPI对接表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform_basicOpenapi_add')" )
    public R save(@RequestBody BasicOpenapiEntity basicOpenapi) {
        return R.ok(basicOpenapiService.save(basicOpenapi));
    }

    /**
     * 修改OpenAPI对接表
     * @param basicOpenapi OpenAPI对接表
     * @return R
     */
    @Operation(summary = "修改OpenAPI对接表" , description = "修改OpenAPI对接表" )
    @SysLog("修改OpenAPI对接表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform_basicOpenapi_edit')" )
    public R updateById(@RequestBody BasicOpenapiEntity basicOpenapi) {
        return R.ok(basicOpenapiService.updateById(basicOpenapi));
    }

    /**
     * 通过id删除OpenAPI对接表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除OpenAPI对接表" , description = "通过id删除OpenAPI对接表" )
    @SysLog("通过id删除OpenAPI对接表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform_basicOpenapi_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(basicOpenapiService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param basicOpenapi 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('platform_basicOpenapi_export')" )
    public List<BasicOpenapiEntity> export(BasicOpenapiEntity basicOpenapi,Long[] ids) {
        return basicOpenapiService.list(Wrappers.lambdaQuery(basicOpenapi).in(ArrayUtil.isNotEmpty(ids), BasicOpenapiEntity::getId, ids));
    }
}